#include "camerawindow.h"

CameraWindow::CameraWindow(CvCapture *cam, QWidget *parent) : QWidget(parent) {

    camera = cam;
    QVBoxLayout *layout = new QVBoxLayout;
    cvwidget = new QOpenCVWidget(this);
    QLabel *label = new QLabel(this);
    imagelabel = new QLabel(this);
    label->setText("Pictures of me");
    snapButton = new QPushButton(this);
    snapButton->setText("Take Picture");
    QHBoxLayout *imagesLayout= new QHBoxLayout;
    imagesLayout->addWidget(cvwidget);

    img.load(":/icarus/user.png");
    //ui->profilePicture->setPixmap(QPixmap::fromImage(myImage));


    imagelabel->setPixmap(QPixmap::fromImage(img));
    imagesLayout->addWidget(imagelabel);


    QHBoxLayout *controlsLayout = new QHBoxLayout;
    controlsLayout->addWidget(snapButton);
    cancelButton = new QPushButton(this);
    cancelButton->setText("Cancel");
    controlsLayout->addWidget(cancelButton);
    layout->addWidget(label);
    //layout->addWidget(cvwidget);
    layout->addLayout(imagesLayout);
    layout->addLayout(controlsLayout);
    setLayout(layout);
    //resize(500, 400);
    timerId = startTimer(100);
    connect(snapButton, SIGNAL(clicked()),this,SLOT(takePicture()));
    connect(cancelButton, SIGNAL(clicked()),this,SLOT(cancelPicture()));

 }

void CameraWindow::timerEvent(QTimerEvent*) {
    IplImage *image=cvQueryFrame(camera);
    cvwidget->putImage(image);
}

CameraWindow::~CameraWindow(){

    qDebug("deleting camerawindow");
    cvReleaseCapture(&this->camera);

}

void CameraWindow::stopTimer(){
    killTimer(timerId);
}

void CameraWindow::takePicture(){
    imageTaken = cvQueryFrame(camera);
    stopTimer();
    emit error("picture taked");
    QImage* image = cvImageToQImage(imageTaken);


    img = image->scaled(QSize(160,160), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    //img = image;

    imagelabel->setPixmap(QPixmap::fromImage(img));

    image->save("/home/havena/test.png");
    cancelPicture();
}

void CameraWindow::cancelPicture(){
    timerId = startTimer(100);
}

QImage* CameraWindow::cvImageToQImage(IplImage *iplImg){

    int h = iplImg->height;
    int w = iplImg->width;
    int channels = iplImg->nChannels;
    QImage *qimg = new QImage(w, h, QImage::Format_ARGB32);
    char *data = iplImg->imageData;

    for (int y = 0; y < h; y++, data += iplImg->widthStep) {
        for (int x = 0; x < w; x++) {
            char r, g, b, a = 0;
            if (channels == 1) {
                r = data[x * channels];
                g = data[x * channels];
                b = data[x * channels];
            }
            else if (channels == 3 || channels == 4) {
                r = data[x * channels + 2];
                g = data[x * channels + 1];
                b = data[x * channels];
            }

            if (channels == 4) {
                a = data[x * channels + 3];
                qimg->setPixel(x, y, qRgba(r, g, b, a));
            }
            else {
                qimg->setPixel(x, y, qRgb(r, g, b));
            }
        }
    }
    return qimg;
}
